﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Diagnostics;
using System.IO;
using Microsoft.Xna.Framework.Storage;

namespace ItemCreatorTest.source
{
    public partial class Form1 : Form
    {
        string mFilename;
        string mItemPath = "C:\\Users\\Steven\\Desktop\\proj\\exodus\\source\\VS 2010\\XML\\items";
        XmlSerializer serializer;

        public Form1()
        {
            InitializeComponent();
            serializer = new XmlSerializer(typeof(ItemDefinition));
            this.Text = "Exodus Item Creator v1.0";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (nameBox.Text != "")
            {
                ItemDefinition item = new ItemDefinition();
                FillInItemFromData(item);
                mFilename = "../../../../source/VS 2010/XML/items/" + (nameBox.Text.ToLower().Replace(" ", "_")) + ".xml";
                FileStream stream = new FileStream(mFilename, FileMode.OpenOrCreate);
                using (stream)
                {
                    serializer.Serialize(stream, item);
                    errorBox.ForeColor = Color.Black;
                    char[] charsToTrim = {'.', '/'};
                    errorBox.Text = item.mName + " saved to " + mFilename.Trim(charsToTrim);
                    errorBox.Visible = true;
                }
            }
            else
            {
                errorBox.Text = "You must enter a name for your item before saving...";
                errorBox.Visible = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            nameBox.ResetText();
            descriptionBox.ResetText();
            flavorBox.ResetText();
            strengthValue.Value = 0;
            agilityValue.Value = 0;
            powerValue.Value = 0;
            speedValue.Value = 0;
            attackValue.Value = 0;
            defenseValue.Value = 0;
            staminaValue.Value = 0;
            critValue.Value = 0;
            errorBox.Text = "";
            errorBox.Visible = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            loadItemDialog.InitialDirectory = mItemPath;
            loadItemDialog.ShowDialog(this);
        }

        private void loadItemDialog_FileOk(object sender, CancelEventArgs e)
        {
            string filename = loadItemDialog.FileName;
            Stream stream = new FileStream(filename, FileMode.Open);
            using (stream)
            {
                ItemDefinition temp = (ItemDefinition)serializer.Deserialize(stream);
                FillInDataFromItem(temp);
            }
        }

        private void FillInDataFromItem(ItemDefinition item)
        {
            nameBox.Text        = item.mName;
            descriptionBox.Text = item.mDescription;
            flavorBox.Text      = item.mFlavor;
            strengthValue.Value = item.mStrength;
            agilityValue.Value  = item.mAgility;
            powerValue.Value    = item.mPower;
            speedValue.Value    = item.mSpeed ;
            attackValue.Value   = item.mAtk;
            defenseValue.Value  = item.mDef;
            staminaValue.Value  = item.mSta;
            critValue.Value     = item.mCrt;
        }

        private void FillInItemFromData(ItemDefinition item)
        {
            item.mName = nameBox.Text;
            item.mDescription = descriptionBox.Text;
            item.mFlavor = flavorBox.Text;
            item.mStrength = (int)strengthValue.Value;
            item.mAgility = (int)agilityValue.Value;
            item.mPower = (int)powerValue.Value;
            item.mSpeed = (int)speedValue.Value;
            item.mAtk = (int)attackValue.Value;
            item.mDef = (int)defenseValue.Value;
            item.mSta = (int)staminaValue.Value;
            item.mCrt = (int)critValue.Value;
        }


    }
}
